package com.lovo.psc.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lovo.commom.dto.VendorProductDto;
import com.lovo.psc.bean.OrderBean;
import com.lovo.psc.mapper.IOrderMapper;
import com.lovo.psc.service.IOrderService;
import com.lovo.psc.vo.OrderVo;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class IOrderServiceImpl implements IOrderService {
    @Autowired
    private IOrderMapper mapper;

    @Override
    public List<OrderBean> findAll() {
        return mapper.selectList(null);
    }

    @Override
    public List<OrderVo> findByOrderId(int id) {
        return mapper.findByOrderId(id);
    }

    @Override
    public void updateById(long id,int status) {
        OrderBean orderBean = mapper.selectById(id);
        orderBean.setStatus(status);
        mapper.updateById(orderBean);
    }

    @Override
    public List<OrderBean> findByBox(String name, int status) {
        return mapper.findByBox(name, status);
    }

    @Override
    public OrderBean findById(long id) {
        QueryWrapper<OrderBean> qw = new QueryWrapper<>();
        qw.eq("pk_orderId",id);
        return mapper.selectOne(qw);
    }

    @Override
    public int chaYi(long orderId) {
        OrderBean orderBean = mapper.selectById(orderId);
        return orderBean.getRest();
    }

    @Override
    public void jieSuan(long orderId, int num,float price) {
        OrderBean orderBean = mapper.selectById(orderId);
        orderBean.setRest(orderBean.getRest() - num);
        orderBean.setGetMoney(orderBean.getGetMoney()+price);
        mapper.updateById(orderBean);

    }

    @RabbitListener(queues = "queueCaiGou")
    public void caiGou(String info){
        VendorProductDto dto = JSONObject.parseObject(info, VendorProductDto.class);
        OrderBean orderBean = new OrderBean(dto.getIdNum(),dto.getNum(),dto.getNum(),dto.getMoney());
        mapper.insert(orderBean);
    }




}
